package com.mycontentprovider1;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import androidx.annotation.Nullable;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 张英杰 on 2017/4/27.
 *
 *
 *
 * !!! 很详细，待看
 * http://gityuan.com/2016/07/30/content-provider/
 * 参考：
 * contentprovider
 * https://blog.csdn.net/liuhe688/article/details/7050868
 *
 * new cursor
 * https://blog.csdn.net/zhang_jun_ling/article/details/51288441
 *
 *
 TODO activity
 Uri.Builder builder = new Uri.Builder();
 Uri buildUri = builder.scheme("content")
 .authority(AUTHORITY)
 .appendPath("getall")
 .appendQueryParameter("faceLeftPointX", faceLeftPointX + "")
 .appendQueryParameter("faceLeftPointY", faceLeftPointY + "")
 .appendQueryParameter("faceRightPointX", faceRightPointX + "")
 .appendQueryParameter("faceRightPointY", faceRightPointY + "")
 .build();
 String temp = mContext.getContentResolver().getType(buildUri);


 TODO provider
 public String getType(@NonNull Uri uri) {
 String path = uri.getPath();
 if (path.equals("/set")) {
 Log.e("TemperatureActivity", "0 设置相机参数 "+System.currentTimeMillis());
 faceLeftPointX = uri.getQueryParameter("faceLeftPointX");
 faceLeftPointY = uri.getQueryParameter("faceLeftPointY");
 faceRightPointX = uri.getQueryParameter("faceRightPointX");
 faceRightPointY = uri.getQueryParameter("faceRightPointY");


 */
public class MContentProvider extends ContentProvider {
    private static final String TAG = "MContentProvider";
    private static final UriMatcher matcher;

    //content://com.mycontentprovider1/student/3
    //content://   schema
    //com.mycontentprovider1   authority
    //student/3    path
    //3    id

    private static final String AUTHORITY = "com.mycontentprovider1";

    private static final int PERSON_ALL = 0;
    private static final int PERSON_ONE = 1;
    private static final int LIGHT = 2;

    static {
        matcher = new UriMatcher(UriMatcher.NO_MATCH);

        matcher.addURI(AUTHORITY, "persons", PERSON_ALL);	//匹配记录集合
        //Cursor c = resolver.query(PERSON_ALL_URI, null, null, null, null);
        matcher.addURI(AUTHORITY, "persons/#", PERSON_ONE);	//匹配单条记录
        //Uri personOneUri = ContentUris.withAppendedId(PERSON_ALL_URI, 1);查询_id为1的记录

        matcher.addURI(AUTHORITY, "lightswitch/#", LIGHT);	//灯光
    }

    @Override
        public boolean onCreate() {
        Log.i(TAG, "onCreate: thread "+Thread.currentThread().getName());
        // TODO: 2020/3/20  true false 影响？
        Log.i(TAG, "onCreate: return "+true );
        return true;
    }


    @Nullable
    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        Log.i(TAG, "query: thread "+Thread.currentThread().getName());

        int match = matcher.match(uri);
        Log.i(TAG, "query: match "+match);
        String[] columnNames = new String[1];
        columnNames[0] = "result";
        List<Object> listObj = new ArrayList<>();
        boolean mRes = true;
        String msg = "结果 1";
        Object[] objects = {"{" +
                "result:" + mRes + "," +
                "message:" + msg +
                "}"};
        listObj.add(objects);
        Cursor cursor = getCursor(columnNames,listObj);
        switch (match){
            case PERSON_ALL:
                break;
            case PERSON_ONE:
                long _id = ContentUris.parseId(uri);
                /**
                 *      源码
                 *     public static long parseId(Uri contentUri) {
                 *         String last = contentUri.getLastPathSegment();
                 *         return last == null ? -1 : Long.parseLong(last);
                 *     }
                 */
                selection = "_id = ?";
                Log.i(TAG, "query: person id "+_id);
                break;
            case LIGHT:
                break;
            default:
                throw new IllegalArgumentException("Unknown URI: " + uri);
        }
//        return getCursor();
        return cursor;
    }


    private Cursor getCursor() {
        String[] columnNames = new String[]{"_id","姓名","班级","学号"};
        MatrixCursor cursor = new MatrixCursor(columnNames);
        for(int j = 0; j<10; j++){
            cursor.addRow(getColumnData());
        }
        return cursor;
    }
    private static int count = 0;
    public static Object[] getColumnData(){
        count++;
        return new Object[]{Integer.valueOf(count),"zhangsan"+count,"jisuanji"+count,"201210"+count};
    }
    private Cursor getCursor(String[] columnNames, List<Object> listObj) {
        if (columnNames == null || listObj ==null) {
            return null;
        }
        MatrixCursor cursor = new MatrixCursor(columnNames);
        for(int j = 0; j<listObj.size(); j++){
            cursor.addRow((Object[]) listObj.get(j));
        }
        return cursor;
    }


    @Nullable
    @Override
    public String getType(Uri uri) {
        Log.i(TAG, "getType: thread "+Thread.currentThread().getName());

        return null;
    }

    @Nullable
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        Log.i(TAG, "insert: thread "+Thread.currentThread().getName());
        return null;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        Log.i(TAG, "delete: thread "+Thread.currentThread().getName());
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        Log.i(TAG, "update: thread "+Thread.currentThread().getName());
        return 0;
    }
}
